const Tank = require('TankExplode')

cc.Class({
    extends: cc.Component,

    properties: {
        tank: cc.Prefab
    },

    // use this for initialization
    onLoad() {
        // 开启碰撞检测
        this.node.on('stanby', this.stanby, this)
        this.spawnTank()    // 生成坦克
    },
    // 镜头回到初始位置
    stanby() {
        this.spawnTank()
    },
    // 生成坦克
    spawnTank() {
        let tank
        if (cc.pool.hasObject(Tank)) {
            tank = cc.pool.getFromPool(Tank).node
        }
        else {
            tank = cc.instantiate(this.tank)
        }
        this.node.addChild(tank)
        tank.setPosition(this.nodeSpace(cc.p(960, 187)))
        tank.getComponent(Tank).init()
    },
    // 将坐标点转换为相对节点左下角的坐标系
    nodeSpace(pos) {
        let self = this.node
        let difference = cc.pSub(pos, self.convertToNodeSpace(cc.p(0, 0)))
        return self.convertToNodeSpaceAR(difference)
    },
})
